package com.dlb.andorid.activity;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dlb.andorid.R;
import com.dlb.andorid.XContract;
import com.dlb.andorid.bean.MyAddress;
import com.dlb.andorid.bean.PeripheryShop;
import com.dlb.andorid.bean.ProductBean;
import com.dlb.andorid.bean.ServerResult;
import com.dlb.andorid.presenter.ZbhdPresenter;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonActivity;

import java.util.ArrayList;
import java.util.List;

public class ZbhdActivity extends BaseCommonActivity implements XContract.IZbhdView {

	ZbhdPresenter mPresenter;
	long address_id = 0;

	@Override
	protected BasePresenter getBasePresenter() {
		mPresenter = new ZbhdPresenter(this);
		return mPresenter;
	}

	@Override
	protected void initData() {
		mPresenter.getZbhdList(address_id);
	}

	@Override
	protected int getTopViewId() {
		return 0;
	}

	@Override
	protected int getMainViewId() {
		return R.layout.activity_zbhd;
	}

	@Override
	protected int getBottomViewId() {
		return 0;
	}

	@Override
	protected String getTitleTxt() {
		return "周边好店";
	}

	@Override
	public String getPageName() {
		return null;
	}

	TextView tv_name, tv_addr;
	private View ll_address;

	RecyclerView recyclerView;
	BaseQuickAdapter<PeripheryShop, BaseViewHolder> mAdapter;
	List<PeripheryShop> mPeripheryShops = new ArrayList<>();

	@Override
	protected void initViews() {
		super.initViews();

		tv_name = findViewById(R.id.tv_name);
		tv_addr = findViewById(R.id.tv_addr);

		ll_address = findViewById(R.id.ll_address);
		ll_address.setOnClickListener(this);

		recyclerView = findViewById(R.id.recycleView);
		recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
		recyclerView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		recyclerView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		recyclerView.setLayerType(View.LAYER_TYPE_NONE, null);
		recyclerView.setHasFixedSize(true);
		recyclerView.setItemAnimator(null);
		recyclerView.setNestedScrollingEnabled(false);

		mAdapter = new BaseQuickAdapter<PeripheryShop, BaseViewHolder>(R.layout.list_item_zbhd, mPeripheryShops) {
			@Override
			protected void convert(BaseViewHolder helper, PeripheryShop item) {
				mImageLoader.loadUrl(item.getPeriphery_shop_pic(), (ImageView) helper.getView(R.id.iv_icon));
				helper.setText(R.id.tv_name, item.getPeriphery_shop_name());
			}
		};

		mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				Bundle bundle = new Bundle();
				bundle.putSerializable("data",mPeripheryShops.get(position));
				go2Activity(ZbhdInfoActivity.class,bundle,false);
			}
		});

		recyclerView.setAdapter(mAdapter);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (v == ll_address) {
			go2ActivityForResult(AddressListActivity.class, 100, null, 0, 0);
		}
	}

	@Override
	public void getZbhdListSuccess(List<PeripheryShop> data) {

		if (data != null) {
			mPeripheryShops.clear();
			mPeripheryShops.addAll(data);
			mAdapter.notifyDataSetChanged();
		}
	}

	@Override
	public void getZbhdInfoSuccess(List<ProductBean> data) {

	}

	@Override
	public void addCarSuccess(ServerResult data) {

	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.e("kwan", "onActivityResult");
		if (requestCode == 100) {//选择地址返回
			MyAddress mMyAddress = (MyAddress) data.getBundleExtra("data").getSerializable("address");
			tv_name.setText(mMyAddress.getName() + " " + mMyAddress.getPhone());
			tv_addr.setText(mMyAddress.getProvince() + mMyAddress.getCity() + mMyAddress.getCity() + " " + mMyAddress.getAreainfo());
			address_id = mMyAddress.getAddress_id();
			mPresenter.getZbhdList(address_id);
		}
	}
}
